<template>
  <el-dialog
    v-model="dialogVisible"
    title="报告详情"
    width="80%"
    :before-close="handleClose"
    class="report-detail-dialog"
  >
    <div class="report-detail">
      <!-- 报告基本信息 -->
      <el-card class="basic-info-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>报告基本信息</span>
            <div class="header-actions">
              <el-tag :type="getStatusType(reportData.status)" size="large">
                {{ getStatusText(reportData.status) }}
              </el-tag>
            </div>
          </div>
        </template>
        
        <div class="info-grid">
          <div class="info-item">
            <span class="label">报告编号：</span>
            <span class="value">{{ reportData.reportId }}</span>
          </div>
          <div class="info-item">
            <span class="label">患者姓名：</span>
            <span class="value">{{ reportData.patientName }}</span>
          </div>
          <div class="info-item">
            <span class="label">性别：</span>
            <span class="value">{{ reportData.gender || '男' }}</span>
          </div>
          <div class="info-item">
            <span class="label">年龄：</span>
            <span class="value">{{ reportData.age || '35' }}岁</span>
          </div>
          <div class="info-item">
            <span class="label">体检日期：</span>
            <span class="value">{{ formatDate(reportData.checkupDate) }}</span>
          </div>
          <div class="info-item">
            <span class="label">报告日期：</span>
            <span class="value">{{ formatDate(reportData.reportDate) }}</span>
          </div>
          <div class="info-item">
            <span class="label">主检医生：</span>
            <span class="value">{{ reportData.primaryDoctor }}</span>
          </div>
          <div class="info-item">
            <span class="label">审核医生：</span>
            <span class="value">{{ reportData.reviewDoctor }}</span>
          </div>
        </div>
      </el-card>
      
      <!-- 体检结果 -->
      <el-card class="results-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>体检结果</span>
            <div class="header-actions">
              <el-button size="small" @click="handleExportResults">
                <el-icon><Download /></el-icon>
                导出结果
              </el-button>
            </div>
          </div>
        </template>
        
        <div class="results-summary">
          <div class="summary-stats">
            <div class="stat-item normal">
              <div class="stat-number">{{ normalCount }}</div>
              <div class="stat-label">正常</div>
            </div>
            <div class="stat-item abnormal">
              <div class="stat-number">{{ abnormalCount }}</div>
              <div class="stat-label">异常</div>
            </div>
            <div class="stat-item total">
              <div class="stat-number">{{ totalCount }}</div>
              <div class="stat-label">总计</div>
            </div>
          </div>
        </div>
        
        <div class="results-table">
          <el-table :data="reportData.results" stripe style="width: 100%">
            <el-table-column prop="category" label="分类" width="100">
              <template #default="{ row }">
                <el-tag size="small" :type="getCategoryType(row.category)">
                  {{ getCategoryText(row.category) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="itemName" label="检查项目" min-width="150" />
            <el-table-column prop="value" label="检查结果" min-width="120" />
            <el-table-column prop="referenceRange" label="参考范围" min-width="120" />
            <el-table-column prop="abnormal" label="异常标识" width="100">
              <template #default="{ row }">
                <el-tag :type="getAbnormalType(row.abnormal)" size="small">
                  {{ getAbnormalText(row.abnormal) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" min-width="150" show-overflow-tooltip />
          </el-table>
        </div>
      </el-card>
      
      <!-- 总结与建议 -->
      <el-card class="summary-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>总结与建议</span>
          </div>
        </template>
        
        <div class="summary-content">
          <div class="summary-section">
            <h4 class="summary-title">
              <el-icon><DocumentChecked /></el-icon>
              总体评价
            </h4>
            <div class="summary-text">
              {{ reportData.summary?.overall || '暂无总体评价' }}
            </div>
          </div>
          
          <div class="summary-section" v-if="reportData.summary?.abnormalities">
            <h4 class="summary-title abnormal">
              <el-icon><WarningFilled /></el-icon>
              主要异常
            </h4>
            <div class="summary-text abnormal">
              {{ reportData.summary.abnormalities }}
            </div>
          </div>
          
          <div class="summary-section" v-if="reportData.summary?.suggestions">
            <h4 class="summary-title suggestion">
              <el-icon><Lightbulb /></el-icon>
              健康建议
            </h4>
            <div class="summary-text suggestion">
              {{ reportData.summary.suggestions }}
            </div>
          </div>
        </div>
      </el-card>
      
      <!-- 详细分析 -->
      <el-card class="analysis-card" shadow="never" v-if="reportData.analysis">
        <template #header>
          <div class="card-header">
            <span>详细分析</span>
          </div>
        </template>
        
        <div class="analysis-content">
          <pre>{{ reportData.analysis }}</pre>
        </div>
      </el-card>
      
      <!-- 注意事项 -->
      <el-card class="notes-card" shadow="never" v-if="reportData.notes">
        <template #header>
          <div class="card-header">
            <span>注意事项</span>
          </div>
        </template>
        
        <div class="notes-content">
          <pre>{{ reportData.notes }}</pre>
        </div>
      </el-card>
      
      <!-- 操作记录 -->
      <el-card class="history-card" shadow="never">
        <template #header>
          <div class="card-header">
            <span>操作记录</span>
          </div>
        </template>
        
        <el-timeline>
          <el-timeline-item
            v-for="(record, index) in operationHistory"
            :key="index"
            :timestamp="formatDateTime(record.time)"
            :type="getTimelineType(record.type)"
          >
            <div class="timeline-content">
              <div class="timeline-title">{{ record.title }}</div>
              <div class="timeline-description">{{ record.description }}</div>
              <div class="timeline-operator">操作人：{{ record.operator }}</div>
            </div>
          </el-timeline-item>
        </el-timeline>
      </el-card>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
        <el-button @click="handlePreview">
          <el-icon><View /></el-icon>
          预览
        </el-button>
        <el-button @click="handlePrint">
          <el-icon><Printer /></el-icon>
          打印
        </el-button>
        <el-button type="primary" @click="handleEdit">
          <el-icon><Edit /></el-icon>
          编辑
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

// Props
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  reportData: {
    type: Object,
    default: () => ({})
  }
})

// Emits
const emit = defineEmits(['update:visible', 'edit', 'preview', 'print'])

const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
})

// 计算统计数据
const normalCount = computed(() => {
  return props.reportData.results?.filter(item => item.abnormal === 'normal').length || 0
})

const abnormalCount = computed(() => {
  return props.reportData.results?.filter(item => item.abnormal !== 'normal').length || 0
})

const totalCount = computed(() => {
  return props.reportData.results?.length || 0
})

// 模拟操作记录
const operationHistory = ref([
  {
    type: 'create',
    title: '报告创建',
    description: '系统自动生成体检报告',
    operator: '系统',
    time: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000)
  },
  {
    type: 'edit',
    title: '报告编辑',
    description: '主检医生完善报告内容',
    operator: '张医生',
    time: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000)
  },
  {
    type: 'review',
    title: '报告审核',
    description: '审核医生审核通过',
    operator: '李医生',
    time: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000)
  },
  {
    type: 'publish',
    title: '报告发布',
    description: '报告正式发布',
    operator: '系统',
    time: new Date()
  }
])

// 方法
const handleClose = () => {
  dialogVisible.value = false
}

const handleEdit = () => {
  emit('edit', props.reportData)
}

const handlePreview = () => {
  emit('preview', props.reportData)
}

const handlePrint = () => {
  emit('print', props.reportData)
}

const handleExportResults = () => {
  ElMessage.info('导出功能开发中')
}

const formatDate = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString('zh-CN')
}

const formatDateTime = (date) => {
  if (!date) return ''
  return new Date(date).toLocaleString('zh-CN')
}

const getStatusType = (status) => {
  const statusMap = {
    draft: 'info',
    pending: 'warning',
    reviewed: 'primary',
    published: 'success'
  }
  return statusMap[status] || 'info'
}

const getStatusText = (status) => {
  const statusMap = {
    draft: '草稿',
    pending: '待审核',
    reviewed: '已审核',
    published: '已发布'
  }
  return statusMap[status] || '未知'
}

const getCategoryType = (category) => {
  const categoryMap = {
    blood: 'danger',
    urine: 'warning',
    liver: 'success',
    kidney: 'primary',
    ecg: 'info',
    chest: '',
    ultrasound: 'success',
    other: 'info'
  }
  return categoryMap[category] || 'info'
}

const getCategoryText = (category) => {
  const categoryMap = {
    blood: '血常规',
    urine: '尿常规',
    liver: '肝功能',
    kidney: '肾功能',
    ecg: '心电图',
    chest: '胸片',
    ultrasound: 'B超',
    other: '其他'
  }
  return categoryMap[category] || '其他'
}

const getAbnormalType = (abnormal) => {
  const abnormalMap = {
    normal: 'success',
    high: 'danger',
    low: 'warning',
    abnormal: 'danger'
  }
  return abnormalMap[abnormal] || 'success'
}

const getAbnormalText = (abnormal) => {
  const abnormalMap = {
    normal: '正常',
    high: '偏高',
    low: '偏低',
    abnormal: '异常'
  }
  return abnormalMap[abnormal] || '正常'
}

const getTimelineType = (type) => {
  const typeMap = {
    create: 'primary',
    edit: 'warning',
    review: 'success',
    publish: 'success'
  }
  return typeMap[type] || 'primary'
}
</script>

<style scoped>
.report-detail-dialog :deep(.el-dialog__body) {
  padding: 20px;
  max-height: 80vh;
  overflow-y: auto;
}

.report-detail {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.basic-info-card,
.results-card,
.summary-card,
.analysis-card,
.notes-card,
.history-card {
  border: 1px solid #e4e7ed;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  color: #2c3e50;
}

.header-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* 基本信息 */
.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.info-item {
  display: flex;
  align-items: center;
  padding: 8px 0;
}

.info-item .label {
  font-weight: 500;
  color: #6c757d;
  min-width: 80px;
}

.info-item .value {
  color: #2c3e50;
  font-weight: 500;
}

/* 结果统计 */
.results-summary {
  margin-bottom: 20px;
}

.summary-stats {
  display: flex;
  gap: 24px;
  justify-content: center;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.stat-item {
  text-align: center;
  padding: 16px 24px;
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  min-width: 80px;
}

.stat-item.normal {
  border-left: 4px solid #67c23a;
}

.stat-item.abnormal {
  border-left: 4px solid #f56c6c;
}

.stat-item.total {
  border-left: 4px solid #409eff;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 14px;
  color: #6c757d;
}

/* 总结内容 */
.summary-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.summary-section {
  padding: 16px;
  border-radius: 8px;
  border: 1px solid #e4e7ed;
}

.summary-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 12px;
}

.summary-title.abnormal {
  color: #f56c6c;
}

.summary-title.suggestion {
  color: #67c23a;
}

.summary-text {
  line-height: 1.8;
  color: #495057;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 6px;
  border-left: 4px solid #409eff;
}

.summary-text.abnormal {
  background: #fff5f5;
  border-left-color: #f56c6c;
}

.summary-text.suggestion {
  background: #f0fff4;
  border-left-color: #67c23a;
}

/* 详细分析和注意事项 */
.analysis-content,
.notes-content {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 6px;
  border: 1px solid #e4e7ed;
}

.analysis-content pre,
.notes-content pre {
  margin: 0;
  font-family: inherit;
  white-space: pre-wrap;
  word-wrap: break-word;
  color: #495057;
  line-height: 1.8;
}

/* 时间线 */
.timeline-content {
  padding: 8px 0;
}

.timeline-title {
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 4px;
}

.timeline-description {
  color: #6c757d;
  margin-bottom: 4px;
}

.timeline-operator {
  font-size: 12px;
  color: #adb5bd;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .report-detail-dialog {
    width: 95% !important;
  }
  
  .report-detail-dialog :deep(.el-dialog__body) {
    padding: 15px;
  }
  
  .card-header {
    flex-direction: column;
    gap: 8px;
    align-items: flex-start;
  }
  
  .header-actions {
    width: 100%;
    justify-content: flex-end;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .summary-stats {
    flex-direction: column;
    gap: 12px;
  }
  
  .stat-item {
    min-width: auto;
  }
  
  .dialog-footer {
    flex-direction: column;
  }
  
  .dialog-footer .el-button {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .summary-stats {
    padding: 15px;
  }
  
  .stat-number {
    font-size: 20px;
  }
  
  .summary-title {
    font-size: 14px;
  }
  
  .timeline-content {
    padding: 6px 0;
  }
}
</style>